layui.use(['form', 'table', 'upload'], function () {
    var form = layui.form,
        layer = layui.layer,
        table = layui.table,
        $ = layui.$,
        upload = layui.upload;

    /**
     * 初始化表单，要加上，不然刷新部分组件可能会不加载
     */
    form.render();

    // 当前弹出层，防止ID被覆盖
    var parentIndex = layer.index;

    //初始化
    $(function(){
        var id = $("#id").val();
        if(id != '' && id != null && id != undefined){
            var index = layer.load(2);
            var url = "/web/boIndexImage/getInfoById";
            $.get(url, {id: id}, function(data){
                layer.close(index);
                if(data.state == "200"){
                    $("#sort").val(data.sort);
                    $("#index_image").attr('src', '/index_image/' + data.url);
                    $("#indexImagePath").val(data.url);
                    $("#thumUrl").val(data.thumUrl);
                    $("#imgBtn").hide(); //隐藏上传按钮
                    if(data.isShow == '1'){
                        $("#isShowSwitch").removeAttr("checked");
                        $("#isShow").val('1');
                    }
                    $("#remark").val(data.remark);
                }else{
                    layer.msg(data.message, {icon:2});
                }
                form.render();
            })
        }
    });

    //点击取消
    $("#closeBtn").on("click", function(){
        var id = $("#id").val();
        if(id == null || id == '' || id == undefined){ //编辑时不操作删除
            deleteImgLog();
        }
        // 关闭弹出层
        layer.close(parentIndex);
    });

    //点击右上角关闭
    $(".layui-layer-close1").on("click", function(){
        var id = $("#id").val();
        if(id == null || id == '' || id == undefined){ //编辑时不操作删除
            deleteImgLog();
        }
        // 关闭弹出层
        layer.close(parentIndex);
    });

    //不保存，点击取消则删除编辑器已经上传的图片
    function deleteImgLog(){
        var indexImagePath = $("#indexImagePath").val(); //原图
        var thumUrl = $("#thumUrl").val(); //缩略图
        var url = "/web/boIndexImage/deleteImgLog";
        $.post(url, {indexImagePath: indexImagePath, thumUrl: thumUrl}, function(res){
            if(res.state == "200"){
                layer.msg(res.message, {icon:1});
            }else{
                layer.msg(res.message, {icon:2});
            }
        }, 'json');
    }

    //监听是否显示开关
    form.on('switch(isShowSwitch)', function(data){
        if(data.elem.checked){
            $("#isShow").val("0"); //选中
        }else {
            $("#isShow").val("1");
        }
    });

    //照片上传
    var uploadInst = upload.render({
        elem: '#imgBtn'
        ,url: '/web/boIndexImage/uploadIndexImage' //改成您自己的上传接口
        ,accept: 'images'
        ,before: function(obj){
            layer.load(2);
            //预读本地文件示例，不支持ie8
            obj.preview(function(index, file, result){
                $('#index_image').attr('src', result); //图片链接（base64）
            });
        }
        ,done: function(res){
            layer.closeAll('loading'); //关闭loading
            //上传成功
            if(res.state == "200"){
                $("#indexImagePath").val(res.fileName); //图片路径
                $("#imgSize").val(res.fileSize); //图片大小
                $("#thumUrl").val(res.thumFileName); //缩略图地址
                $("#thumSize").val(res.thumFileSize); //缩略图大小
                return layer.msg(res.message, {icon:1});
                //如果上传失败
            }else{
                return layer.msg(res.message, {icon:2});
            }
        }
        ,error: function(){
            layer.closeAll('loading'); //关闭loading
            //演示失败状态，并实现重传
            var demoText = $('#index_image_text');
            demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs head-reload">重试</a>');
            demoText.find('.head-reload').on('click', function(){
                uploadInst.upload();
            });
        }
    });

    //监听提交
    form.on('submit(saveBtn)', function (data) {
        var index = layer.load(2);
        var url = "/web/boIndexImage/save";
        $.post(url, data.field, function(res){
            if(res.state == "200"){
                layer.msg(res.message, {icon:1});
            }else{
                layer.msg(res.message, {icon:2});
            }
            // 关闭弹出层
            layer.close(index);
            layer.close(parentIndex);
        }, 'json');

        return false;
    });

});